package com.springboot.quanxian.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.quanxian.common.DictionaryEnum;
import com.springboot.quanxian.common.ResultEnum;
import com.springboot.quanxian.entity.*;
import com.springboot.quanxian.entity.req.SysManagerReq;
import com.springboot.quanxian.entity.search.SysManagerSearch;
import com.springboot.quanxian.entity.view.SysManagerRoleView;
import com.springboot.quanxian.entity.view.SysManagerView;
import com.springboot.quanxian.exception.MyException;
import com.springboot.quanxian.mapper.SysManagerMapper;
import com.springboot.quanxian.qpage.QPage;
import com.springboot.quanxian.service.*;
import com.springboot.quanxian.util.IdentityUtil;
import com.springboot.quanxian.util.ResultView;
import com.springboot.quanxian.util.RsaUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 后台账户表 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-06-16
 */
@Service
public class SysManagerServiceImpl extends ServiceImpl<SysManagerMapper, SysManager> implements ISysManagerService {

    @Autowired
    private ISysManagerRoleService iHjManagerRoleService;

    @Autowired
    private ISysRoleService iHjRoleService;


    /**
     * 员工列表
     * @param qPage
     * @param obj
     * @return
     */
    @Override
    public ResultView getListAll(QPage qPage, SysManagerSearch obj) {
        IPage iPage = new Page(qPage.getOffset(), qPage.getLimit());
        List<SysManagerView> mapList = this.baseMapper.getPageList(iPage,obj);
        return ResultView.ok(iPage.setRecords(mapList));
    }

    /**
     * 用户不分页列表
     * @return
     */
    @Override
    public ResultView getSelectList(SysManagerSearch obj) {
        List<SysManagerView> sysManagerViews=this.baseMapper.getSelectList(obj);
        return ResultView.ok(sysManagerViews);
    }

    /**
     * 用户详情
     * @param managerId
     * @return
     */
    @Override
    public ResultView getManagerId(String managerId) {
        SysManagerView sysManagerView=this.baseMapper.getManagerId(managerId);
        return ResultView.ok(sysManagerView);
    }

    @Override
    public ResultView restPwd(String managerId, String mPwd,String againPwd) {
        if(!mPwd.equals(againPwd)){
            return ResultView.error("两次输入的密码不一致");
        }
        SysManager sysManager=this.getById(managerId);
        if(sysManager==null){
            throw new MyException(ResultEnum.CODE_2,"数据异常");
        }
        sysManager.setMPwd(RsaUtil.setRsaPwd(mPwd));
        boolean isOk=this.updateById(sysManager);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"重置密码数据异常");
        }
        return ResultView.ok();
    }


    /**
     * 检测当前账户是否存在
     * @param account
     * @return
     */
    @Override
    public SysManager exitAccount(String account,String roleId) {
        QueryWrapper<SysManager> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("m_account",account);
        queryWrapper.eq("role_id",roleId);
        SysManager hjManager=this.getOne(queryWrapper);
        return hjManager;
    }


    @Transactional
    @Override
    public ResultView updatePwd(String managerId,String pwd,String newPwd,String againPwd) {
        if(!newPwd.equals(againPwd)){
            return ResultView.error("两次输入的新密码不一致");
        }
        SysManager hjManager=this.getById(managerId);
        if(hjManager==null){
            throw new MyException(ResultEnum.CODE_2,"获取当前用户信息数据异常");
        }
        if(!pwd.equals(RsaUtil.getRsaPwd(hjManager.getMPwd()))){
            throw new MyException(ResultEnum.CODE_2,"旧密码认证异常，修改密码失败");
        }
        hjManager.setMPwd(RsaUtil.setRsaPwd(newPwd));
        hjManager.setUpdateTime(new Date());
        boolean isOk=this.updateById(hjManager);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"数据异常，修改密码失败");
        }
        return ResultView.ok();
    }

    @Transactional
    @Override
    public ResultView add(SysManager obj) {
        if(exitAccount(obj.getMAccount(),obj.getRoleId())!=null){
            throw new MyException(ResultEnum.CODE_2,"创建账号异常，账号已存在");
        }
        SysRole sysRole=this.iHjRoleService.getById(obj.getRoleId());
        obj.setRoleName(sysRole.getRoleName());
        obj.setAddTime(new Date());
        obj.setMPwd(RsaUtil.setRsaPwd(obj.getMPwd()));
        obj.setAddManagerId(StpUtil.getLoginIdAsString());
        obj.setManagerId(IdentityUtil.identityId("MA"));
        boolean isOk=this.save(obj);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"数据异常，新增账号失败");
        }
        //新增用户角色关联表
        isOk=this.iHjManagerRoleService.saveRoleUser(obj.getRoleId(),obj.getManagerId());
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"添加用户角色关联出现异常");
        }
        return ResultView.ok();
    }

    @Transactional
    @Override
    public ResultView up(SysManager obj) {
        SysManager hjManager=this.getById(obj.getManagerId());
        BeanUtils.copyProperties(obj,hjManager,new String[]{"managerId"});
        hjManager.setMPwd(RsaUtil.setRsaPwd(obj.getMPwd()));
        boolean count=this.updateById(hjManager);
        if(!count){
            throw new MyException(ResultEnum.CODE_2,"修改账户出现异常");
        }
        //删除之前关联这个账户关联的角色数据
        this.iHjManagerRoleService.removeManagerRoleId(obj.getManagerId());
        //新增用户角色关联表
        boolean isOk=this.iHjManagerRoleService.saveRoleUser(obj.getRoleId(),obj.getManagerId());
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"添加用户角色关联出现异常");
        }
        return ResultView.ok();
    }

    /**
     * 注册
     * @param obj
     * @return
     */
    @Override
    public ResultView register(SysManager obj) {
        if(exitAccount(obj.getMAccount(),obj.getRoleId())!=null){
            throw new MyException(ResultEnum.CODE_2,"注册账号异常，账号已存在");
        }
        SysRole sysRole=this.iHjRoleService.getById(obj.getRoleId());
        obj.setRoleName(sysRole.getRoleName());
        obj.setAddTime(new Date());
        obj.setMPwd(RsaUtil.setRsaPwd(obj.getMPwd()));
        obj.setManagerId(IdentityUtil.identityId("MA"));
        boolean isOk=this.save(obj);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"数据异常，注册账号失败");
        }
        //新增用户角色关联表
        isOk=this.iHjManagerRoleService.saveRoleUser(obj.getRoleId(),obj.getManagerId());
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"添加用户角色关联出现异常");
        }
        return ResultView.ok();
    }


}
